import { Component, OnInit } from '@angular/core';
import { uniqueId, cloneDeep } from 'lodash';
import { OptionItem } from '@iux/live';

interface DemoUser {
  id: string;
  name: string;
  age: number;
  expand: boolean;
  random: number;
  disabled: boolean;
  children?: DemoUser[];
  readonly?: boolean;
}

@Component({
  selector: 'app-demo2',
  templateUrl: './demo2.component.html',
  styles: []
})
export class Demo2Component implements OnInit {
  data: OptionItem[];
  value: { [key: string]: any };
  index = 0;
  constructor() {}

  ngOnInit() {
    this.initData();
  }

  initData() {
    this.data = Array.from({ length: 10 }).map((item, key) => {
      return this.initUser(key, key % 2 !== 0);
    });

    const children = Array.from({ length: 3 }).map((item, key) => {
      return this.initUser(key + 11, key % 2 !== 0);
    });

    this.data[0].isLeaf = false;
    this.data[0].children = children;
    this.data[0].readonly = true; // 不能选择，该项readonly设置为true
    this.data[2].children = cloneDeep(children);
    this.data[2].isLeaf = false;

    this.value = this.data[3];
  }

  initUser(key: number, disabled: boolean, readonly?: boolean) {
    return {
      id: uniqueId('uid'),
      label: `user${key}`,
      age: 10 + key,
      expand: false,
      random: parseInt(Math.random() * 100 + '', 10),
      disabled,
      isLeaf: true,
      readonly
    };
  }

  change(value) {
    console.log('change', value);
  }
}
